package com.leetcode.algorithm.y22.m07.w4;

import java.util.ArrayList;
import java.util.List;

/**
 * 1260. 二维网格迁移
 * 
 * https://leetcode.cn/problems/shift-2d-grid/
 * 
 * @author jie.deng
 *
 */
class Question1260Solution01 {

	public List<List<Integer>> shiftGrid(int[][] grid, int k) {
		int rows = grid.length;
		int cols = grid[0].length;
		int nums = rows * cols;
		List<List<Integer>> list = new ArrayList<>();
		List<Integer> tmpList = new ArrayList<>();
		list.add(tmpList);
		k = k % nums;
		for (int idx = nums - k; idx < nums; idx++) {
			int row = idx / cols;
			int col = idx % cols;
			tmpList = list.get(list.size() - 1);
			if (tmpList.size() == cols) {
				tmpList = new ArrayList<>();
				list.add(tmpList);
			}
			tmpList.add(grid[row][col]);
		}
		for (int idx = 0; idx < nums - k; idx++) {
			int row = idx / cols;
			int col = idx % cols;
			tmpList = list.get(list.size() - 1);
			if (tmpList.size() == cols) {
				tmpList = new ArrayList<>();
				list.add(tmpList);
			}
			tmpList.add(grid[row][col]);
		}
		return list;
	}

}